Add multi-configuration support: 2d / 3d_lowres / 3d_cascade_fullres (v1.2.3)#6
Merged
Merged
Conversation
Previously distillation only worked for 3d_fullres. The student model and ONNX export paths hardcoded Conv3d/InstanceNorm3d and a 5D dummy input shape, so any other configuration crashed. Changes: - nnUNetDistillationTrainer: pick conv_op/norm_op from len(patch_size); 2D configs now use Conv2d/InstanceNorm2d. Cascade input channels are handled by upstream determine_num_input_channels (already adds num_classes from the previous stage), so 3d_cascade_fullres works without extra plumbing. - LiteNNUNetStudent / LiteResEncStudent: default kernel/stride/norm pick the right shape and op class from conv_op. norm_op default is now None with auto-selection; existing callers that pass it explicitly stay valid. - ONNX export scripts: detect dim from patch_size, build N+C+spatial dummy input, set dynamic_axes per-dim, and include the configuration in the generated filename so 2d/3d_lowres/cascade don't overwrite each other. - Help text on --configuration now lists all four supported values. - docs/Distillation.md: documents the supported configurations table and adds example commands plus a cascade workflow note. - Bump version to 1.2.3. CPU smoke tests cover 2D and 3D forward/backward for both student types, cascade-shaped (extra input channels) forward, distillation loss, and a 2D ONNX export round-trip (max_diff = 2.6e-06). Full GPU end-to-end training and ONNX export against real checkpoints still need to be run on the GPU box. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
7 tasks
77even
added a commit
that referenced
this pull request
May 12, 2026
Add multi-configuration support: 2d / 3d_lowres / 3d_cascade_fullres (v1.2.3)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Previously distillation only worked for
3d_fullres. The student model and ONNX export paths hardcodedConv3d/InstanceNorm3dand a 5D dummy input shape, so2d,3d_lowres, and3d_cascade_fullresall crashed (issue raised in chat).This PR makes all four standard nnUNetv2 configurations work end-to-end through the distillation + ONNX export path.
Changes
nnUNetDistillationTrainer)conv_op/norm_opfromlen(patch_size): 2D configs useConv2d/InstanceNorm2d, 3D configs useConv3d/InstanceNorm3d.3d_cascade_fullres) input channels come from upstreamdetermine_num_input_channels, which already addsnum_classesfrom the previous stage — no extra plumbing needed.LiteNNUNetStudent,LiteResEncStudent)kernel_sizes/stridesshape andnorm_opclass are picked fromconv_op.norm_opdefault is nowNonewith auto-selection. Existing callers that pass it explicitly stay valid.fast_nnunet_distillation_export_onnx,fast_nnunet_resenc_distillation_export_onnx)patch_size, builds the right N+C+spatial dummy input.dynamic_axesare emitted per-dim (3 spatial axes in 3D, 2 in 2D).model_2d_fold0_2x.onnx) so 2d / 3d_lowres / cascade exports don't overwrite each other.--configurationnow lists all four supported values for the four entry-point scripts.docs/Distillation.md): configurations table, example commands per configuration, cascade workflow note.1.2.2->1.2.3.Test plan
CPU smoke tests (local-only, not committed) — 7/7 pass on macOS:
LiteNNUNetStudentforward / backwardLiteNNUNetStudentforward / backward (verifiesInstanceNorm2dwas chosen)LiteResEncStudentforward / backwardLiteResEncStudentforward / backwarddistillation_loss_fnKL loss + backwardmax_diff = 2.6e-06)GPU validation still needed before tagging the release:
-c 2d,-c 3d_lowres, and-c 3d_cascade_fullres(cascade requires upstreamnnUNetv2_train ... 3d_lowres+nnUNetv2_predict --save_probabilitiesto be run first sopredicted_next_stage/exists).Follow-ups
v1.2.3, publish release notes (plain Markdown, no emojis).refactor/drop-vendored-nnunetv2(PR Drop vendored nnunetv2; depend on upstream pip package (v1.3.0) #4) on top of newmain. The same changes apply to the moved-out trainer file; setup.py conflict should be resolved by keeping1.3.0.